package com.zhixun.mobile.common.utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import com.alibaba.fastjson.JSONObject;

public class SMSUtils {
	
	

	public static void main(String[] args) {
		
//		String url = "http://v.juhe.cn/sms/send?mobile=13731046166&tpl_id=5260&tpl_value=%23code%23%3D234234&key=4022933cb8daf8a05ead7cc9d39d1bdd";//url为请求的api接口地址
//	    String key= "4022933cb8daf8a05ead7cc9d39d1bdd";//申请的对应key
//		String urlAll = new StringBuffer(url).toString(); 
//		String charset ="UTF-8";
//		String jsonResult = get(urlAll, charset);//得到JSON字符串
//		JSONObject object = JSONObject.parseObject(jsonResult);//转化为JSON类
//		String code = object.getString("error_code");//得到错误码
//		//错误码判断
//		if(code.equals("0")){
//			//根据需要取得数据
//			JSONObject jsonObject =  (JSONObject)object.getJSONArray("result").get(0);
//			System.out.println(jsonObject.getJSONObject("citynow").get("AQI"));
//		}else{
//			System.out.println("error_code:"+code+",reason:"+object.getString("reason"));
//		}
		
		String mobile ="18612888501";
		String verifyCode = "123";
//		sendSMSByMap(mobile, verifyCode);
		
		
		
		sendCarByMap("霍俊","130435199208101559");
		
		
	}
	
	/**
	 * @author Jakemanse
	 * @time 2015-8-25  上午11:41:01
	 * @function <p>发送短信  </p>
	 * @param mobile
	 * @param verifyCode
	 * @return
	 */
	public static boolean sendSMS(String mobile,String verifyCode){
//		String url = "http://v.juhe.cn/sms/send?mobile="+mobile+"&tpl_id=21336&tpl_value=%23code%23%3D"+verifyCode+"&key=e21b63572bb6c92399e817f5a99dc9cb";//url为请求的api接口地址
//		String url = "http://v.juhe.cn/sms/send?mobile="+mobile+"&tpl_id=26139&tpl_value=%23code%23%3D"+verifyCode+"&key=e21b63572bb6c92399e817f5a99dc9cb";//url为请求的api接口地址
		String url = "http://v.juhe.cn/sms/send?mobile="+mobile+"&tpl_id=28468&tpl_value=%23code%23%3D"+verifyCode+"&key=7edb621e22d28e423659e585e8c49c1c";//url为请求的api接口地址
		String urlAll = new StringBuffer(url).toString(); 
		String charset ="UTF-8";
		String jsonResult = get(urlAll, charset);//得到JSON字符串
		System.out.println(jsonResult);
		JSONObject object = JSONObject.parseObject(jsonResult);//转化为JSON类
		String code = object.getString("error_code");//得到错误码
		//错误码判断
		if(code.equals("0")){
			//根据需要取得数据
//			JSONObject jsonObject =  (JSONObject)object.getJSONArray("result").get(0);
			return true;
		}else{
			System.out.println("error_code:"+code+",reason:"+object.getString("reason"));
			return false;
		}
	}
	
	public static Map<String, Object> sendSMSByMap(String mobile,String verifyCode){
//		String url = "http://v.juhe.cn/sms/send?mobile="+mobile+"&tpl_id=21336111&tpl_value=%23code%23%3D"+verifyCode+"&key=e21b63572bb6c92sdfgsdfds";//url为请求的api接口地址
//		String url = "http://v.juhe.cn/sms/send?mobile="+mobile+"&tpl_id=26139&tpl_value=%23code%23%3D"+verifyCode+"&key=e21b63572bb6c92399e817f5a99dc9cb";//url为请求的api接口地址
//		String url = "http://v.juhe.cn/sms/send?mobile="+mobile+"&tpl_id=28468&tpl_value=%23code%23%3D"+verifyCode+"&key=7edb621e22d28e423659e585e8c49c1c";//url为请求的api接口地址
//		String url = "http://v.juhe.cn/sms/send?mobile="+mobile+"&tpl_id=34122&tpl_value=%23code%23%3D"+verifyCode+"&key=e21b63572bb6c92399e817f5a99dc9cb";//url为请求的api接口地址
		String url = "http://v.juhe.cn/sms/send?mobile="+mobile+"&tpl_id=96800&tpl_value=%23code%23%3D"+verifyCode+"&key=e21b63572bb6c92399e817f5a99dc9cb";//url为请求的api接口地址
		String urlAll = new StringBuffer(url).toString(); 
		String charset ="UTF-8";
		String jsonResult = get(urlAll, charset);//得到JSON字符串
		System.out.println(jsonResult);
		JSONObject object = JSONObject.parseObject(jsonResult);//转化为JSON类
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("error_code", object.getString("error_code"));
		map.put("reason", object.getString("reason"));
		return map;
	}
	
	
	/**
	    * 
	    * @param urlAll:请求接口
	    * @param charset:字符编码
	    * @return 返回json结果
	    */
	   public static String get(String urlAll,String charset){
		   BufferedReader reader = null;
		   String result = null;
		   StringBuffer sbf = new StringBuffer();
		   String userAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36";//模拟浏览器
		   try {
			   URL url = new URL(urlAll);
			   HttpURLConnection connection = (HttpURLConnection)url.openConnection();
			   connection.setRequestMethod("GET");
			   connection.setReadTimeout(30000);
			   connection.setConnectTimeout(30000);
			   connection.setRequestProperty("User-agent",userAgent);
			   connection.connect();
			   InputStream is = connection.getInputStream();
			   reader = new BufferedReader(new InputStreamReader(
						is, charset));
				String strRead = null;
				while ((strRead = reader.readLine()) != null) {
					sbf.append(strRead);
					sbf.append("\r\n");
				}
				reader.close();
				result = sbf.toString();
			   
		} catch (Exception e) {
			e.printStackTrace();
		}
		   return result;
	   }

		public static String getRandNum(int charCount) {
			String charValue = "";
			for (int i = 0; i < charCount; i++){
				char c = (char) (randomInt(0,10)+'0');
				charValue += String.valueOf(c);
			}
			return charValue;
		}
		public static int randomInt(int from, int to){
			Random r = new Random();
			return from + r.nextInt(to - from);
		}
		
		//////////////////////////////////////////身份证号验证///////////////////////////////////////
		
		//身份证号验证
		/**
		 * name 姓名
		 * idNumber 身份证号
		 * */
		public static Map<String, Object> sendCarByMap(String name,String idNumber){
			try {
				name   =   java.net.URLEncoder.encode(name, "utf-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			
			
			/*
			{
			    "reason": "成功1",
			    "result": {
			        "realname": "董好帅",真实姓名
			        "idcard": "330329199001020012",身份证号码
			        "res": 1 1：匹配 2：不匹配
			    },
			    "error_code": 0
			}*/
			
			
			String url = "http://op.juhe.cn/idcard/query?key=d223d8d1ec05c38426925f3dc49606ed&idcard="+idNumber+"&realname="+name;//url为请求的api接口地址
			String urlAll = new StringBuffer(url).toString(); 
			String charset ="UTF-8";
			String jsonResult = get(urlAll, charset);//得到JSON字符串
			System.out.println(jsonResult);
			JSONObject object = JSONObject.parseObject(jsonResult);//转化为JSON类
			Map<String, Object> map = new HashMap<String,Object>();
			map.put("error_code", object.getString("error_code"));
			map.put("reason", object.getString("reason"));
			
			if("0".equals(object.getString("error_code"))){//调用成功
				String result = object.getString("result");
				JSONObject object1 = JSONObject.parseObject(result);//转化为JSON类
				map.put("res", object1.getString("res"));
			}
			
			
			return map;
		}
   
}
